
class CircleMainPanelAI extends BasePanel {

    btn_list: eui.RadioButton;
    btn_history: eui.RadioButton;
    btn_back: ButtonEx;

    //返回_按钮
    btn_fanhui: eui.Group;
    //刷新_按钮
    btn_update: eui.Image;
    //离开圈子_按钮
    btn_likai: eui.Group;

    //圈内房间数量
    lab_number: eui.Label;
    //圈子ID
    lab_id: eui.Label;

    group_list: eui.Group;

    nokai_item: CircleMainItem;

    scroller: eui.Scroller;

    pageTool: PageTool;

    lab_nulltip: eui.Label;
    img_nulltip: eui.Image;

    lab_title: eui.Label;
    /**牌局和战绩标签页 */
    group_tab: eui.Group;
    /**圈名 */
    lab_quan_name: eui.Label;
    /**历史战绩房间 */
    room_info: CircleDetailsInfoItem;
    /**房卡状态 */
    icon_fangka: eui.Image;
    lab_fangka: eui.Label;
    private fkConf = {
        0: ["0X8e0303", "btn_tab_red", "不足"],
        1: ["0Xa14213", "btn_tab_orange", "正常"],
        2: ["0X006000", "btn_tab_green", "充足"]
    }

    constructor() {
        super();
        this.skinName = "CircleMainPanel";
    }
    // ui: CircleMainUI;

    timestamp: number;

    page: number;

    history_list: any;
    details_list: any;



    createChildren() {

        super.createChildren();
        ButtonEx.draw(this.btn_fanhui);
        ButtonEx.draw(this.btn_update);
        ButtonEx.draw(this.btn_likai);
        this.room_info.host = this;
        //this.ui.btn_fanhui.visible = false;
        this.btn_fanhui.addEventListener(egret.TouchEvent.TOUCH_TAP, this.click_handler, this);
        this.btn_list.addEventListener(egret.TouchEvent.TOUCH_TAP, this.list_handler, this);
        this.btn_history.addEventListener(egret.TouchEvent.TOUCH_TAP, this.list_handler, this);
        this.btn_likai.addEventListener(egret.TouchEvent.TOUCH_TAP, this.click_handler, this);
        this.btn_back.addEventListener(egret.TouchEvent.TOUCH_TAP, this.click_handler, this);


        this.btn_update.addEventListener(egret.TouchEvent.TOUCH_TAP, this.click_handler, this);

        gameManager.i.addEventListener(GameEventType.Circle_2015, this.update, this);

        gameManager.i.addEventListener(GameEventType.Circle_2016, this.update_history, this);

        gameManager.i.addEventListener(GameEventType.Circle_2023, this.update, this);

        gameManager.i.addEventListener(GameEventType.UPDATE_RECORD_DETAILS_LIST, this.update_details, this);

        this.pageTool.addEventListener(CommonEventType.CHANGED, this.onUpdatePage, this);


        Circle.button_cold_click(this.btn_update, 1000);
        Circle.button_cold_click(this.btn_list, 1000);
        Circle.button_cold_click(this.btn_history, 1000);
        Circle.button_cold_click(this.pageTool.leftBtnDisplay, 1000);
        Circle.button_cold_click(this.pageTool.rightBtnDisplay, 1000);


    }
    //上下翻页点击
    private onUpdatePage() {
        if (this.page != this.pageTool.page) {
            this.page = this.pageTool.page;

            let start: number = (this.page - 1) * game.Count_Daikai_History_Page;
            let end: number = start + game.Count_Daikai_History_Page - 1;
            gameManager.socketManager.send(
                2016,
                {
                    id: Circle.args_2015.id,
                    uid: Circle.args_2015.uid,
                    owner_uid: Circle.args_2015.owner_uid,
                    start: start,
                    end: end,
                    time: this.timestamp
                }
            );
        }
    }
    click_handler(e: egret.TouchEvent) {
        if (!game.isBtnDisabledByTime) game.isBtnDisabledByTime = true;
        else return;
        switch (e.currentTarget) {

            case this.btn_fanhui://左上角离开按钮，回到进入列表界面
                this.hide();
                Circle.circle_send(2012, { uid: game.player.uid });
                break;
            case this.btn_update:
                this.clean();
                Circle.circle_send(2015, Circle.args_2015);
                break;
            case this.btn_back:
                this.detailsBack();
                break;
            case this.btn_likai: //退出圈子
                game.askPanel.showMsg(function (r) {
                    if (r) {
                        Circle.circle_send(
                            2019,
                            {
                                id: Circle.args_2015.id,
                                uid: Circle.args_2015.uid,
                            }
                        );
                    }
                }, '您确定离开圈子吗？\n击确定后发送申请，圈主同意或等待48小时后即可离开圈子。');

                //this.hide();
                break;
        }
    }

    list_handler(e: egret.TouchEvent) {
        switch (e.currentTarget) {
            case this.btn_list:
                this.skinState = 'list';
                this.clean();
                Circle.circle_send(2015, Circle.args_2015);
                break;
            case this.btn_history:
                this.skinState = 'history';
                this.clean();
                this.timestamp = 0;
                this.page = 1;
                this.pageTool.setPage(this.page);
                Circle.circle_send(
                    2016,
                    {
                        id: Circle.args_2015.id,
                        uid: Circle.args_2015.uid,
                        owner_uid: Circle.args_2015.owner_uid,
                        start: 0,
                        end: game.Count_Daikai_History_Page - 1,
                        time: this.timestamp
                    }
                );
                break;
        }
    }

    /**战绩返回 */
    detailsBack() {
        this.skinState = 'history';
        this.clean();

        let start: number = (this.page - 1) * game.Count_Daikai_History_Page;
        let end: number = start + game.Count_Daikai_History_Page - 1;

        Circle.circle_send(
            2016,
            {
                id: Circle.args_2015.id,
                uid: Circle.args_2015.uid,
                owner_uid: Circle.args_2015.owner_uid,
                start: start,
                end: end,
                time: this.timestamp
            }
        );
    }


    ifownSet(ifown: any) {

        Circle.circle_data.ifown = ifown;
        //圈主显示战绩按钮，不显示退出圈子按钮
        this.group_tab.visible = <boolean>ifown;
        this.btn_likai.visible = !<boolean>ifown;

    }

    tabSelect(flag: boolean) {
        this.btn_list.selected = flag;
        this.btn_history.selected = !flag;
    }

    update(data: any) {
        if (!this.checkVis()) return;
        if (this.skinState !== "list") return;

        if (data.hasOwnProperty("ifown")) this.ifownSet(data.ifown);

        if (data.hasOwnProperty("cardnow")) this.setFangka(data.cardnow);

        this.nokai_item.update('current', data.nokai);

        this.tabSelect(true);

        if (data.already) {

            this.clean();
            this.lab_quan_name.text = data.ifown == 1 ? data.name + "(我的)" : data.name + "";
            this.lab_title.text = data.name;
            this.lab_number.text = "成员：" + data.pernum + "/" + data.maxnum;
            this.lab_id.text = "圈子ID：" + data.id;

            let item: CircleMainItem;
            let list: any[];
            //
            for (var i = 0; i < data.already.length; i++) {
                item = new CircleMainItem();
                this.group_list.addChild(item);
                item.update('list', data.already[i]);
                item.rank = i + 1;
            }
        }

    }
    update_history(data: any) {
        if (!this.checkVis()) return;
        this.tabSelect(false);


        if (data.hasOwnProperty('zong_shu')) {
            this.pageTool.setScope(1, Math.ceil(+data.zong_shu / game.Count_Daikai_History_Page));
        }

        let list: any[] = [];

        if (data.hasOwnProperty('return_data')) {
            list = data.return_data;
        }
        else {
            list = data;
        }

        if (!list || list.length <= 0) {

            this.lab_nulltip.text = "当前没有任何战绩记录";
            this.img_nulltip.visible = true;
            return;
        }

        this.lab_nulltip.text = "";
        this.img_nulltip.visible = false;
        this.clean();

        if (this.timestamp == 0 && list.length > 0) {
            this.timestamp = list[0].time;
        }

        this.history_list = list;

        let item: CircleMainItem;
        for (let i: number = 0; i < list.length; i++) {
            item = new CircleMainItem();
            this.group_list.addChild(item);
            item.update('history', list[i]);
            item.rank = i + 1 + (this.page - 1) * game.Count_Daikai_History_Page;
        }


    }
    update_details(list: any[]) {
        if (!this.checkVis()) return;
        this.tabSelect(false);
        this.skinState = "details";
        if (!list) return;
        this.clean();
        this.room_info.update(list);
        this.details_list = list;
        let item: CircleMainItem;
        for (let i: number = 0; i < list.length; i++) {
            item = new CircleMainItem();
            this.group_list.addChild(item);
            item.update('details', list[i]);
            item.rank = i + 1;
        }
    }

    private setFangka(num: number) {
        let idx: number = 0;
        if (num <= 200) {
            idx = 0;
        } else if (num < 500) {
            idx = 1;
        } else if (num > 500) {
            idx = 2;
        }
        const [strokeColor, src, text] = this.fkConf[idx];
        this.icon_fangka.source = src;
        this.lab_fangka.strokeColor = strokeColor;
        this.lab_fangka.text = text;
    }

    clean() {
        if (this.scroller == null) { return; }
        this.scroller.stopAnimation();
        this.group_list.scrollV = 0;
        this.group_list.removeChildren();
        this.lab_quan_name.text = "";
    }



    public hide(): void {
        super.hide();
    }

    public close(): void {

        super.hide();
    }

    public show(eff): void {
        super.show(eff);
        // this.m_dialog._title.source = "";
        this.skinState = "list";
        game.roomState = RoomState.CIRCLE;
    }

    private checkVis(): boolean {
        return this.parent != null;
    }
}